
import os
import sys
import pandas as pd
from src.missing.fill_methods import process_missing_values

# 设置项目根目录
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))
if BASE_DIR not in sys.path:
    sys.path.insert(0, BASE_DIR)

# 缺失值文件目录
input_dir = os.path.join(BASE_DIR, "data", "2_missing_value_analysis", "filled", "to_fill_by_indicator")
output_root = os.path.join(BASE_DIR, "data", "2_missing_value_analysis", "filled")

# 遍历所有 *_missing_data.csv 文件
for file_name in os.listdir(input_dir):
    if file_name.endswith("_missing_data.csv"):
        indicator_name = file_name.replace("_missing_data.csv", "").strip()
        confirm = input(f"是否需要对 {indicator_name} 指标进行缺失值填充？请按 Y 或 N 键: ").strip().upper()
        if confirm != "Y":
            continue

        file_path = os.path.join(input_dir, file_name)
        df = pd.read_csv(file_path, encoding="utf-8-sig")

        # 国家列表
        country_list = df['国名Ch'].dropna().unique()
        indicator_list = [indicator_name]

        print(f"开始处理：{indicator_name}，共 {len(country_list)} 个国家")

        # 执行缺失值处理
        process_missing_values(df, indicator_list, country_list, output_root)
